package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.annotation.LogAnno;
import com.example.common.result.Result;
import com.example.controller.base.BaseController;
import com.example.entity.Operate;
import com.example.service.IOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author daiweiyuan
 * @date 2022/4/5
 */
@RestController
@RequestMapping("/operateLog")
public class OperateLogController extends BaseController {

    @Autowired
    public IOperLogService operLogService;

    @GetMapping("/page")
    @PreAuthorize("hasRole('superAdmin')")
    public Result page(int size, int current, String operator) {
        Page<Operate> operatePage = new Page<>(current, size);
        QueryWrapper<Operate> wrapper = new QueryWrapper<>();
        wrapper.likeRight("operator", operator).orderByDesc("create_time");
        return Result.ok().data(this.operLogService.page(operatePage, wrapper));
    }


    @LogAnno(operateType = "删除日志记录")
    @PostMapping("/remove")
    @PreAuthorize("hasRole('superAdmin')")
    public Result remove(@RequestBody List<Integer> ids) {
        if (this.operLogService.removeByIds(ids)) {
            return Result.deleted();
        } else {
            return Result.unDeleted();
        }
    }
}
